package com.ruoyi.hotel.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hotel.domain.HotelRoom;
import com.ruoyi.hotel.util.SubscriptionMessageUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hotel.domain.HotelUser;
import com.ruoyi.hotel.service.IHotelUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 酒店管理Controller
 *
 * @author ruoyi
 * @date 2023-04-12
 */
@RestController
@RequestMapping("/hotel/manage")
public class HotelUserController extends BaseController
{
    @Autowired
    private IHotelUserService hotelUserService;


    /**
     * 查询酒店管理列表
     */
    @PreAuthorize("@ss.hasPermi('hotel:manage:list')")
    @GetMapping("/list")
    public TableDataInfo list(HotelUser hotelUser)
    {
        startPage();
        List<HotelUser> list = hotelUserService.selectHotelUserList(hotelUser);
        return getDataTable(list);
    }


    /**
     * 查询酒店管理列表(下拉框 列表有分页 下拉框不需要分页)
     */
    @PreAuthorize("@ss.hasPermi('hotel:manage:list')")
    @GetMapping("/selectHotelUserListOption")
    public AjaxResult selectHotelUserListOption(HotelUser hotelUser)
    {
        List<HotelUser> list = hotelUserService.selectHotelUserList(hotelUser);
        return AjaxResult.success(list);
    }

    /**
     * 导出酒店管理列表
     */
    @PreAuthorize("@ss.hasPermi('hotel:manage:export')")
    @Log(title = "酒店管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HotelUser hotelUser)
    {
        List<HotelUser> list = hotelUserService.selectHotelUserList(hotelUser);
        ExcelUtil<HotelUser> util = new ExcelUtil<HotelUser>(HotelUser.class);
        util.exportExcel(response, list, "酒店管理数据");
    }

    /**
     * 获取酒店管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('hotel:manage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hotelUserService.selectHotelUserById(id));
    }

    /**
     * 新增酒店管理
     */
    @PreAuthorize("@ss.hasPermi('hotel:manage:add')")
    @Log(title = "酒店管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HotelUser hotelUser)
    {
        return toAjax(hotelUserService.insertHotelUser(hotelUser));
    }

    /**
     * 修改酒店管理
     */
    @PreAuthorize("@ss.hasPermi('hotel:manage:edit')")
    @Log(title = "酒店管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HotelUser hotelUser)
    {
        return toAjax(hotelUserService.updateHotelUser(hotelUser));
    }

    /**
     * 删除酒店管理
     */
    @PreAuthorize("@ss.hasPermi('hotel:manage:remove')")
    @Log(title = "酒店管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hotelUserService.deleteHotelUserByIds(ids));
    }

    /**
     * 查询酒店列表
     */
    @PreAuthorize("@ss.hasPermi('hotel:manage:add')")
    @GetMapping("/db/search")
    public TableDataInfo dataList(HotelUser hotelUser)
    {
        List<HotelUser> hotelSearchDomainList = new ArrayList();
        if(StringUtils.isEmpty(hotelUser.getKeyword())){
            return getDataTable(hotelSearchDomainList);
        }
        hotelSearchDomainList = hotelUserService.searchHotel(hotelUser);
        return getDataTable(hotelSearchDomainList);
    }


    /**
     * 添加酒店（保存）
     */
    @PreAuthorize("@ss.hasPermi('hotel:manage:add')")
    @Log(title = "添加酒店", businessType = BusinessType.INSERT)
    @PostMapping("/addHotel")
    public AjaxResult addHotelSave(String params)
    {
        String[] hotelArr = Convert.toStrArray(params);
        List<HotelUser> hotelSearchDomainList = new ArrayList<HotelUser>();
        for(int i=0;i<hotelArr.length;i++){
            HotelUser hotelSearchDomainTable = new HotelUser();
            String[] hoteObjArr = Convert.toStrArray("\\$",hotelArr[i]);
            hotelSearchDomainTable.setCityName(hoteObjArr[0]);
            hotelSearchDomainTable.setHotelId(hoteObjArr[1]);
            hotelSearchDomainTable.setName(hoteObjArr[2]);
            hotelSearchDomainTable.setHotelGroup(hoteObjArr[3]);
            hotelSearchDomainList.add(hotelSearchDomainTable);
        }
        // 添加酒店信息
        hotelUserService.addHotel(hotelSearchDomainList);
        return success();
    }
}
